package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.constant.CupsConfigConstant;
import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.common.constant.CupsTreeNodeConstant;
import cc.rengu.igas.channel.cups.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BaseManageService;
import cc.rengu.igas.channel.cups.facade.request.CupsSignInRequest;
import cc.rengu.igas.channel.cups.facade.response.CupsSignInResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;

/**
 * 机构发起签到
 *
 * @author Administrator
 */
public class CupsSignInService extends BaseManageService<CupsSignInRequest, CupsSignInResponse> {
    @Override
    protected CupsSignInRequest beforeBizProcess() throws Exception {
        CupsSignInRequest cupsSignInRequest = new CupsSignInRequest();
        ConvertUtil.convertOutput(cupsSignInRequest);
        return cupsSignInRequest;
    }

    @Override
    protected ChnlBizResponse<CupsSignInResponse> callBizService(CupsSignInRequest request) throws Exception {
        return directCups(request);
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse<CupsSignInResponse> response) throws Exception {
        CupsSignInResponse cupsSignInResponse = response.getResult();
        ConvertUtil.convertInput(cupsSignInResponse);
    }

    /**
     * @param cupsSignInRequest 签到请求
     * @return 渠道业务应答（签到应答）
     * @throws Exception 失败
     */
    ChnlBizResponse<CupsSignInResponse> directCups(CupsSignInRequest cupsSignInRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        ChnlBizResponse<CupsSignInResponse> chnlBizResponse = new ChnlBizResponse<>();
        CupsSignInResponse cupsSignInResponse = new CupsSignInResponse();
        SysParamService sysParamService = new SysParamServiceImpl();
        String instId = cupsSignInRequest.getHeader().getInstId();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, CupsConfigConstant.CUPS_CONF_NODE, CupsConfigConstant.CUPS_FWD_INS_CODE);
        if (null == sysParam || StringUtil.isEmptyOrNull(sysParam.getParamValue())) {
            rglog.error("未获取到<{}>机构的发送机构标识", instId);
            throw new BizException(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode(), OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
        }
        String fwdInstCode = sysParam.getParamValue();
        String traceNo = cupsSignInRequest.getHeader().getTraceNo();
        String transDateTime = cupsSignInRequest.getHeader().getTransDate().substring(4, 8) +
                cupsSignInRequest.getHeader().getTransTime();
        String cupsSsn = cupsSignInRequest.getHeader().getTransTime();
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(CupsTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);

        CupsField cupsField = new CupsField();
        BeanUtil.beanCopy(cupsSignInRequest, cupsField);
        cupsField.setMsgType("0820");
        cupsField.setTransDateTime(transDateTime);
        cupsField.setCupsTraceNo(cupsSsn);
        cupsField.setFwdInstCode(fwdInstCode);
        cupsField.setManageNetCode(cupsSignInRequest.getSignType());
        CupsFieldBean cupsFieldBean = new CupsFieldBean();
        cupsFieldBean.setCupsField(cupsField);
        ConvertUtil.convertInput(cupsFieldBean);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, cupsSignInRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        String tunlSsn = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM);
        rglog.info("tunlSsn:[{}]", tunlSsn);
        UnifiedCache.remove(CupsParamConstant.CACHE_CUPS_SN, tunlSsn);

        if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
            rglog.error("调用银联cups服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            cupsSignInResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            cupsSignInResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(cupsSignInResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联cups服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }

        CupsFieldRspBean cupsFieldRspBean = new CupsFieldRspBean();
        ConvertUtil.convertOutput(cupsFieldRspBean);
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, cupsSignInResponse);
        cupsSignInResponse.setRespCode(cupsFieldRsp.getRespCode());

        if (CupsParamConstant.SUCCESS_CODE.equals(cupsSignInResponse.getRespCode())) {
            cupsSignInResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            cupsSignInResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(cupsSignInResponse);
        return chnlBizResponse;
    }

}
